<template>
  <section>
    <!--工具条 高级查询位置-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keywords" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordsQuery">关键字查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <!-- :data="department"  绑定的数据
            highlight-current-row 高亮显示
            v-loading="listLoading" 忙等框
            @selection-change="selsChange"  勾选复选框触发事件
            sortable  排序
            :formatter="formatSex" 操作单选框的事件
    -->
    <el-table :data="logininfos" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="35">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="90" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" sortable>
      </el-table-column>
      <el-table-column prop="salt" label="盐值" width="150" sortable>
      </el-table-column>
      <el-table-column prop="password" label="密码" width="150" sortable>
      </el-table-column>
      <el-table-column prop="type" label="类型" width="100" sortable>
        <template scope="scope">
          <el-span style="color: green" v-if="scope.row.type==1" size="small" >普通用户</el-span>
          <el-span style="color: red" v-else-if="scope.row.type==0" size="small" >管理用户</el-span>
          <el-span style="color: gray" v-else size="small" >未知</el-span>
        </template>
      </el-table-column>
      <el-table-column prop="disable" label="状态" width="100" sortable>
        <template scope="scope">
          <el-span style="color: green" v-if="scope.row.disable==1" size="small" >正常</el-span>
          <el-span style="color: red" v-else-if="scope.row.disable==0" size="small" >停用</el-span>
          <el-span style="color: gray" v-else size="small" >未知</el-span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
      </el-pagination>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="true">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="盐值" prop="salt">
          <el-input v-model="editForm.salt" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" >
          <el-radio-group v-model="editForm.type">
            <el-radio class="radio" :label="0">管理用户</el-radio>
            <el-radio class="radio" :label="1">普通用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" >
          <el-radio-group v-model="editForm.disable">
            <el-radio class="radio" :label="1">正常</el-radio>
            <el-radio class="radio" :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>


<script>

export default {
  data() {
    return {

      //高级查询所需数据
      filters:{
        keywords: null,
      },
      

      //分页显示所属数据
      logininfos: [],
      totals: 0,
      currentPage: 1,
      pageSize: 5,
      listLoading: false,

      //多条数据选中
      sels: [],//列表选中列

      //添加或修改数据
      title:'',
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,

      //表单校验规则
      editFormRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
      },
      //编辑界面数据
      editForm: {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
        type: null,
        disable: 1,
      },

    }
  },
  methods: {
    //关键字查询
    keywordsQuery(){
      //页码改成1
      this.currentPage = 1;
      this.getLogininfos();
    },

    //分页查询 - 点击某一页查询
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      //重新发送请求
      this.getLogininfos();
    },

    //获取用户列表
    getLogininfos() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.filters.keywords  //高级查询才用
      };

      //开启忙等框
      this.listLoading = true;
      //发送请求  axios 需要配置axios全局
      this.$http.post("/logininfo", para)
          .then(res => {  //res: PageList: {totals:xx, rows: xx}

            //关闭忙等框
            this.listLoading = false;

            this.logininfos = res.data.rows;
            this.totals = res.data.totals;

          }).catch(res =>{
        this.$message.error('系统繁忙,稍后重试');
      })

    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {//点击确认按钮
        this.listLoading = true;//忙等框

        //发送请求
        this.$http.delete("/logininfo/"+row.id)
            .then(res=>{

              this.listLoading = false;//关闭忙等框

              if(res.data.success){
                this.$message({
                  message: '删除成功',
                  type:'success'
                });

              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              //重新发送请求,刷新页面  
              this.getLogininfos();

            }).catch(res=>{
          this.$message({
            message: '系统繁忙,稍后重试',
            type: 'error'
          });
        });
      }).catch(() => {//点击取消
        this.$message.error('系统繁忙,稍后重试');
        type:'error'
      });
    },
    
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";
      //弹出模态框
      this.editFormVisible = true;
      //克隆一份数据,然后回显数据
      this.editForm = Object.assign({}, row);
    },
    
    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      this.editFormVisible = true;//弹出模态框
      //清空模态框
      this.editForm= {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
        type: null,
        disable: 1,
      }
    },
    
    //编辑
    //提交按钮
    editSubmit: function () {
      //表单校验:valid是一个布尔值,当校验成功为true,进入到if循环
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          //按下提交之后的弹出确认框
          this.$confirm('确认提交吗？', '温馨提示', {})
              .then(() => {
                this.editLoading = true;//开启忙等框

                //克隆数据,将表单数据克隆一份赋值给para
                let para = Object.assign({}, this.editForm);

                //发送异步请求,put类型,参数为para
                this.$http.put("/logininfo",para)
                    .then(res=>{

                      //关闭忙等框
                      this.editLoading = false;

                      //获取到返回的json数据里的data.success的值:true/false
                      if(res.data.success){ //data.success的值:true进入这个if判断
                        this.$message({
                          message: '操作成功',//前台页面显示数据给用户看
                          type: 'success'
                        });
                      }else(  //data.success的值:false进入这个if判断
                          this.$message({
                            message: res.data.msg,  ////获取到返回的json数据里的data.msg的报错信息显示
                            type: 'error'
                          })
                      )

                      //关闭模态框
                      this.editFormVisible = false;

                      //刷新页面,重新查询一次数据,调用getDepartments()方法
                      this.getLogininfos();

                    }).catch(res=>{
                  this.$message({
                    message: '系统异常,稍后重试',
                    type: 'error'
                  });
                })

              });
        }
      });
    },

    //多选框
    selsChange: function (sels) {
      this.sels = sels;
      //console.debug(sels)
    },

    //批量删除
    batchRemove: function () {

      var ids = this.sels.map(item => item.id);  // [1,2,3,4,5,6]

      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 开启忙等框

        //patch:批量操作的请求
        this.$http.patch("/logininfo",ids)
            .then(res=>{
              // 关闭忙等框
              this.listLoading = false;
              if(res.data.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              this.currentPage = 1;
              this.getLogininfos();
            }).catch(res=>{
          this.$message({
            message: '系统异常，稍后重试',
            type: 'error'
          });
        })
      }).catch(() => {

      });
    }
  },

  //钩子函数:页面加载方法
  mounted() {
    this.getLogininfos();
  }
}

</script>


<style scoped>

</style>